package com.qing.cheng.service.impl;


import com.qing.cheng.dto.QcCourseDto;
import com.qing.cheng.dto.QcStudentDto;
import com.qing.cheng.entity.QcClass;
import com.qing.cheng.entity.QcStudent;
import com.qing.cheng.entity.QcUser;
import com.qing.cheng.mapper.QcClassMapper;
import com.qing.cheng.mapper.QcCourseMapper;
import com.qing.cheng.mapper.QcStudentMapper;
import com.qing.cheng.mapper.QcUserMapper;
import com.qing.cheng.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service

public class UserInfoServiceImpl implements UserInfoService {
    @Autowired(required = false)
    private QcUserMapper qcUserMapper;
    @Autowired(required = false)
    private QcStudentMapper qcStudentMapper;
    @Autowired(required = false)
    private QcCourseMapper qcCourseMapper;
    @Autowired(required = false)
    private QcClassMapper qcClassMapper;


    @Override
    public List<QcStudentDto> listAllStudent(Integer userId) {
        ArrayList<QcStudentDto> qcStudentDtoArrayList = new ArrayList<>();
        List<QcStudent> qcStudents = qcStudentMapper.listByUserId(userId);
        for (QcStudent student : qcStudents) {
            QcStudentDto qcStudentDto = new QcStudentDto();
            BeanUtils.copyProperties(student,qcStudentDto);
            QcClass qcClass = qcClassMapper.selectById(student.getClassId());
            qcStudentDto.setQcClass(qcClass);
            qcStudentDtoArrayList.add(qcStudentDto);
        }
        return qcStudentDtoArrayList;
    }

    @Override
    public List<QcCourseDto> listAllCourse(Integer stuId) {
        QcStudent student = qcStudentMapper.selectByPrimaryKey(stuId);
        Integer classId = student.getClassId();
        return qcCourseMapper.listAllByClassId(classId);
    }

    @Override
    public int toLogin(String phoneNo, Integer verfiyNum, HttpServletRequest request)  {
        QcUser qcUser = qcUserMapper.selectByPhoneNo(phoneNo);

        request.getSession().setAttribute("user",qcUser);
        return qcUser.getId();
    }
}
